package com.lemon.smartsafetynew.ui.fragment.task;

import android.content.res.Resources;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TableLayout;

import com.lemon.android.http.LemonRequestParams;
import com.lemon.smartsafety.app.RefreshType;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafety.utils.app.UIHelper;
import com.lemon.smartsafety.view.list.MultiStateView;
import com.lemon.smartsafetynew.entity.User;
import com.lemon.smartsafetynew.entity.event.common.TypeEvent;
import com.lemon.smartsafetynew.entity.event.task.TaskUpdateEvent;
import com.lemon.smartsafetynew.entity.event.user.PageEvent;
import com.lemon.smartsafetynew.entity.task.InspectionVo;
import com.lemon.smartsafetynew.entity.task.TaskBo;
import com.lemon.smartsafetynew.net.services.SafePatrolServices;
import com.lemon.smartsafetynew.ui.activity.task.InspectionTaskDetailActivity;
import com.lemon.smartsafetynew.ui.adapter.task.InspectionTaskAdapter;
import com.lemon.smartsafetynew.ui.adapter.task.InspectionTaskItemAdapter;
import com.lemon.smartsafetynew.ui.fragment.base.BasePullToRefreshFragment;
import com.lemon.smartsafetynew.util.constants.KeyConstants;
import com.zhy.http.okhttp.OkHttpUtils;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import java.util.ArrayList;
import java.util.List;

import kale.adapter.LemonRcvAdapter;

/**
 * Created by JiangAn on 2018/5/7.
 */

public class InspectionTaskFragment extends BasePullToRefreshFragment<InspectionVo, List<InspectionVo>>
        implements RecyclerTClickListener<InspectionVo> {

    private static final String TAG = "InspectionVo";
    private String status = "";
    private User user = null;
    private InspectionTaskAdapter mAdapter;

    @Override
    protected void getBaseArguments() {
        super.getBaseArguments();
        Bundle bundle = getArguments();
        if (bundle != null) {
            status = bundle.getString(KeyConstants.KEY_INSPECTION_TASK_TYPE);
        }
    }

    public static InspectionTaskFragment newInstance(String type) {
        Bundle args = new Bundle();
        args.putString(KeyConstants.KEY_INSPECTION_TASK_TYPE, type);
        InspectionTaskFragment fragment = new InspectionTaskFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public void onPullRefresh() {
        LemonRequestParams params = new LemonRequestParams("");
        params.put("taskStatus", status);
        params.put("pageNum", currentPage);
        params.put("pageSize", pageSize);

        SafePatrolServices
                .init()
                .getInspectionTaskList(RefreshType.REFRESH, params, status);

    }

    @Override
    public void onLoadMore() {
        LemonRequestParams params = new LemonRequestParams("");
        params.put("taskStatus", status);
        params.put("pageNum", currentPage);
        params.put("pageSize", pageSize);

        SafePatrolServices
                .init()
                .getInspectionTaskList(RefreshType.LOAD_MORE, params, status);
    }

    @Override
    public boolean goRefreshOnCreate() {
        //自动加载
        return true;
    }

    @Override
    public LemonRcvAdapter<InspectionVo> getLemonAdapter() {

        return new InspectionTaskItemAdapter(mDataList, this);
    }

     @Override
    public String getHttpTag() {
        return KeyConstants.URL_GET_INSPECTION_TASK_LIST + status;
    }

    @Override
    protected void initViews() {
        super.initViews();
    }

    @Override
    public void onItemClick(InspectionVo data, int position) {
        if(status.equals(TaskBo.TaskBoStatus.WAIT.getValue())){
            data.setExect(true);
            Bundle bundle = new Bundle();
            bundle.putSerializable(KeyConstants.KEY_INSPECTION_TASK_DETAIL, data);
            UIHelper.showActivity(getActivity(), bundle, InspectionTaskDetailActivity.class);
        }else if(status.equals(TaskBo.TaskBoStatus.DURING.getValue())){
            data.setExect(true);
            Bundle bundle = new Bundle();
            bundle.putSerializable(KeyConstants.KEY_INSPECTION_TASK_DETAIL, data);
            UIHelper.showActivity(getActivity(), bundle, InspectionTaskDetailActivity.class);
        }else if(status.equals(TaskBo.TaskBoStatus.FINISH.getValue())){
            data.setExect(false);
            Bundle bundle = new Bundle();
            bundle.putSerializable(KeyConstants.KEY_INSPECTION_TASK_DETAIL, data);
            UIHelper.showActivity(getActivity(), bundle, InspectionTaskDetailActivity.class);
        }else if(status.equals(TaskBo.TaskBoStatus.OVERDATE.getValue())){
            data.setExect(false);
            Bundle bundle = new Bundle();
            bundle.putSerializable(KeyConstants.KEY_INSPECTION_TASK_DETAIL, data);
            UIHelper.showActivity(getActivity(), bundle, InspectionTaskDetailActivity.class);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        //Log.e(TAG, "【onDestroyView】 status = " + status);
        OkHttpUtils.getInstance().cancelTag(KeyConstants.URL_GET_INSPECTION_TASK_LIST + status);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMainThread(TaskUpdateEvent event) {
        doPullRefresh();
    }
}

